<template>
  <el-dialog v-model:visible="modelValue" title="用户信息" @close="resetForm">
    <el-form :model="userData" ref="form">
      <el-form-item label="用户ID" prop="id">
        <el-input v-model="userData.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="name">
        <el-input v-model="userData.name"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="userData.role">
          <el-option label="管理员" value="admin"></el-option>
          <el-option label="用户" value="user"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="resetForm">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue';

const props = defineProps({
  modelValue: Boolean,
  currentUser: Object,
});

const emit = defineEmits(['submit']);

const userData = ref({ id: '', name: '', role: '' });

watch(() => props.currentUser, (newUser) => {
  if (newUser) {
    userData.value = { ...newUser };
  } else {
    resetForm();
  }
});

const resetForm = () => {
  userData.value = { id: '', name: '', role: '' };
};

const submit = () => {
  emit('submit', userData.value);
};
</script>

<style>

</style>